/*
 * UART.c
 *
 *  Created on: 29/12/2012
 *      Author: Bowmer
 */

#include "inc/hw_types.h"
#include "inc/lm4f120h5qr.h"
#include "driverlib/gpio.h"
#include <string.h>



extern volatile unsigned int RxBufferIndex;
extern volatile unsigned char RxBuffer[200];


/* Transmit a string across UART5 */
void UART0_Send(char *pucBuffer, unsigned long TxCount){

	// Send specified number of characters
	while(TxCount--)
	{
		// Put the character into the FIFO, increase the index
		UART0_DR_R = *pucBuffer++;

		//Probably not using the FIFO to its full extent here. This isn't letting it fill up
		while(UART0_FR_R & UART_FR_BUSY);
	}
}



void UART0_Init(){

	// Enable PORTA peripheral
	SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R0;						//Add PORTA to enabled peripherals

	// Configure PORTA
	GPIO_PORTA_DIR_R = (GPIO_PORTA_DIR_R & ~GPIO_PIN_0)|GPIO_PIN_1;	//Pin 0 is an input, 1 is an output. Maintain other bits
	GPIO_PORTA_AFSEL_R |= (GPIO_PIN_0|GPIO_PIN_1);					//Alternate function on pins 0,1
	GPIO_PORTA_PCTL_R |= GPIO_PCTL_PA0_U0RX|GPIO_PCTL_PA1_U0TX;		//Alternate function is UART0
	GPIO_PORTA_DEN_R |= GPIO_PIN_0|GPIO_PIN_1;						//Digital Enable pins 0,1

	// Enable UART0 Peripheral
	SYSCTL_RCGCUART_R |= SYSCTL_RCGCUART_R0;
	GPIO_PORTB_DATA_R ^= GPIO_PIN_1;
	GPIO_PORTB_DATA_R ^= GPIO_PIN_1;

	// Configure UART0
	UART0_CTL_R &= ~0x01;											//Disable UART5
	UART0_IBRD_R = 21;												//Integer part of scaler
	UART0_FBRD_R = 45;												//Divisor part of scaler
	UART0_LCRH_R = 0x0070;											//8N1, Enable FIFO	//This NEEDS to follow the IBRD and FBRD writes
	UART0_CTL_R = 0x0110;											//Enable transmit
	//UART0_CTL_R |= 0x80;											//Enable Loopback
	UART0_CC_R = 0x00;												//Use PLL
	UART0_CTL_R |= 0x01;											//Enable UART5


	// Initialisation Strings
	char beginString[]= "\033[2JStellaris Prime Standing by:\n\0";

	// Send first initialisation string
	UART0_Send(beginString, strlen(beginString));


}


/* Transmit a string across UART5 */
void UART5_Send(char *pucBuffer, unsigned long TxCount){

	// Send specified number of characters
	while(TxCount--)
	{
		// Put the character into the FIFO, increase the index
		UART5_DR_R = *pucBuffer++;

		//Probably not using the FIFO to its full extent here. This isn't letting it fill up
		while(UART5_FR_R & UART_FR_BUSY);
	}
}

void UART5_Buffer_Init(){
	// Clear the receive buffer...
	for(RxBufferIndex = 0; RxBufferIndex<sizeof(RxBuffer); RxBufferIndex++){
		RxBuffer[RxBufferIndex]=0;
	}
	// ...and reset the index
	RxBufferIndex=0;

}



void UART5_Init(){

	// Enable PORTE peripheral
	SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R4;						//Add PORTE to enabled peripherals

	// Configure PORTE
	GPIO_PORTE_DIR_R = (GPIO_PORTE_DIR_R & ~GPIO_PIN_4)|GPIO_PIN_5;	//Pin 4 is an input, 5 is an output. Maintain other bits
	GPIO_PORTE_AFSEL_R |= (GPIO_PIN_4|GPIO_PIN_5);					//Alternate function on pins 4,5
	GPIO_PORTE_PCTL_R |= GPIO_PCTL_PE4_U5RX|GPIO_PCTL_PE5_U5TX;		//Alternate function is UART5
	GPIO_PORTE_DEN_R |= GPIO_PIN_4|GPIO_PIN_5;						//Digital Enable pins 4,5

	// Enable UART5 Peripheral
	SYSCTL_RCGCUART_R |= SYSCTL_RCGCUART_R5;
	GPIO_PORTB_DATA_R ^= GPIO_PIN_1;
	GPIO_PORTB_DATA_R ^= GPIO_PIN_1;

	// Configure UART5
	UART5_CTL_R &= ~0x01;											//Disable UART5
	UART5_IBRD_R = 21;												//Integer part of scaler
	UART5_FBRD_R = 45;												//Divisor part of scaler
	UART5_LCRH_R = 0x0070;											//8N1, Enable FIFO	//This NEEDS to follow the IBRD and FBRD writes
	UART5_CTL_R = 0x0310;											//Enable both transmit and receive
	//UART5_CTL_R |= 0x80;											//Enable Loopback
	UART5_CC_R = 0x00;												//Use PLL
	UART5_CTL_R |= 0x01;											//Enable UART5


	// Initialisation Strings
	char beginString[]= "\033[2JStellaris Prime Standing by:\n\0";
	char beginString2[]= " Ready To Receive on UART0:\n\0";

	// Send first initialisation string
	UART5_Send(beginString, strlen(beginString));

	// Enable UART5 Interrupts in the NVIC
	NVIC_EN1_R |= NVIC_EN0_INT29;
	// Enable Receive interrupts, and Receive Timeout interrupts
	UART5_IM_R = UART_IM_RXIM | UART_IM_RTIM;

	// Send second initialisation string
	UART5_Send(beginString2, strlen(beginString));
}
